package net.huashitong.supervision.insideDuCha.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.insideDuCha.entity.InsideUserTaskEntity;
import net.huashitong.supervision.insideDuCha.filter.InsideUserTaskFilter;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class InsideUserTaskDao extends StringDao<InsideUserTaskEntity> {



    public List<InsideUserTaskEntity>  getUserTasks(InsideUserTaskFilter filter){


        GenericQuery query = createQuery("obj");
        if(StringUtils.isNotBlank(filter.getMainTaskId())){

            query.append(" and obj.mainTaskId =:mainTaskId").setParam("mainTaskId",filter.getMainTaskId());
        }
        if(StringUtils.isNotBlank(filter.getSubTaskId())){

            query.append(" and obj.subTaskId =:subTaskId").setParam("subTaskId",filter.getSubTaskId());
        }

        if(StringUtils.isNotBlank(filter.getUserId())&&StringUtils.isNotBlank(filter.getCreateUser()))
        {
            query.append(" and ( obj.userId=:userId").setParam("userId",filter.getUserId())
            .append(" or obj.createUser=:createUser )").setParam("createUser",filter.getCreateUser());
        }else {

            if(StringUtils.isNotBlank(filter.getUserId())&&StringUtils.isBlank(filter.getCreateUser())){

                query.append(" and obj.userId=:userId").setParam("userId",filter.getUserId());
            }
            if(StringUtils.isNotBlank(filter.getCreateUser())&&StringUtils.isBlank(filter.getUserId())){

                query.append(" and obj.createUser=:createUser").setParam("createUser",filter.getCreateUser());
            }

        }
        //任务状态(0.待签收,1待反馈,2已反馈,3退回,4完成 。5已签收 9 查看自己 10 全部 ，11 未完成的)
        if(StringUtils.isNotBlank(filter.getTaskState()))
        {
            if(!"9".equals(filter.getTaskState())&&!"10".equals(filter.getTaskState())&&!"11".equals(filter.getTaskState())){//9 自己创建的， 10 全部

                query.append(" and obj.taskState=:taskState").setParam("taskState",filter.getTaskState());
            }
            //未完成任务为： 待反馈和以反馈的任务。
            if("11".equals(filter.getTaskState())){
                query.append(" and ( obj.taskState='1' or obj.taskState='2' ) ");

            }
        }
        if(StringUtils.isNotBlank(filter.getDeptId()))
        {
            query.append(" and obj.deptId=:deptId").setParam("deptId",filter.getDeptId());

        }
       /* if("9".equals(filter.getTaskState())){
            query.append( " and obj.taskState<>4");
        }*/
        query.setOrder("sysId",GenericQuery.DESC);
        return query.listResult(filter);

    }


}
